<?php

$admin = true;
require_once('auth_req.inc.php');

function format_m($_val)
{
    if ($_val < 1024)
    {
        return $_val;
    }
    elseif ($_val < (1024 * 1024)) // Kb
    {
        return round( $_val / 1024, 2) . " Кб";
        
    }
    elseif ($_val < (1024 * 1024 * 1024)) // Mb
    {
        return round( $_val / (1024 * 1024), 2) . " Мб";

    }
    else
    {
        return round( $_val / (1024 * 1024 * 1024), 2) . " Гб";
    }
}

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 

$dir = opendir('..' . $path);

while ($dir && ($file = readdir($dir)) !== false) 
{
    $full_file = '..' . $path.'/'.$file;

    if (is_file($full_file) && preg_match("/\.sql\.gz$/", $file))
    {
        $all_list[] = array('name'=>$file, 'size'=>filesize($full_file), 'date'=>date("d.m.Y H:i:s", filemtime($full_file)));
    }
        
}

$count = count($all_list);

if( $count > 0 ) 
{ 
    $total_pages = ceil($count/$limit); 
} 
else 
{ 
    $total_pages = 0; 
} 

if ($page > $total_pages) 
    $page=$total_pages;


$start = $limit * $page - $limit;

if ($start < 0) 
    $start = 0;

$responce->page = $page; 
$responce->total = $total_pages; 
$responce->records = $count; 

for($i = $start; ($i < ($start + $limit)) && ($i < count($all_list)); $i++)
{
    $responce->rows[$i-$start]['id'] = $all_list[$i]['name'];
    $responce->rows[$i-$start]['cell'] = array( $all_list[$i]['name'], format_m($all_list[$i]['size']),$all_list[$i]['date']);
}

echo json_encode($responce);

?>
